﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Threading;
using System.IO;

namespace CnmViewer
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            List<CnmFile> source = new List<CnmFile>();
            foreach (string arg in MyExtensions.Tools.GetCmdArgumentsOnly())
            {
                try
                {
                    FileInfo f = new FileInfo(arg);
                    if (f.Exists)
                        source.Add(
                            new CnmFile(f)
                            );
                }
                catch (Exception exc)
                {
                    MessageBox.Show(
                        arg + "\r\n" + exc.GetType().Name + ": " + exc.Message,
                        "Can't open",
                        MessageBoxButton.OK,
                        MessageBoxImage.Stop
                        );
                }
            }

            if (source.Count > 0)
            {
                lstItems.ItemsSource = source;
                lstItems.SelectedItem = source[0];
            }
        }

        private void cmdOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFolderDialog w = new OpenFolderDialog();
            if (w.ShowDialog() == true)
                lstItems.ItemsSource = w.ResultItems;
        }

        private void lstItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CnmFile cnm = lstItems.SelectedItem as CnmFile;
            Stream html = null;

            if (cnm != null)
            {
                Thread th = new Thread(() =>
                    {
                        if (!cnm.IsParsed)
                            cnm.Parse();

                        if (cnm.TextHtml != null)
                            html = cnm.TextHtml;

                        this.Invoke(() =>
                            {
                                if (object.ReferenceEquals(lstItems.SelectedItem, cnm))
                                {
                                    panelView.DataContext = cnm;
                                    txtHtml.NavigateToStreamOrBlank(html);
                                }
                            });
                    });

                th.Start();
            }
            else
            {
                panelView.DataContext = cnm;
                txtHtml.NavigateToStreamOrBlank(html);
            }
        }

        private void TextBlock_MouseUp(object sender, MouseButtonEventArgs e)
        {
            cmdOpen_Click(null, null);
        }
    }
}
